/**
 * @file boards\gd32f103cb\bsp\bsp_blue_led.c
 *
 * Copyright (C) 2023
 *
 * bsp_blue_led.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author HinsShum hinsshum@qq.com
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "gd32f10x_libopt.h"
#include "gpio_group.h"
#include "options.h"

/*---------- macro ----------*/
#define led_rcu0 RCU_GPIOA
#define led_port0 GPIOA
#define led_pin1 GPIO_PIN_7
#define led_pin8 GPIO_PIN_6
#define radar_pin GPIO_PIN_4

#define led_rcu1 RCU_GPIOB
#define led_port1 GPIOB
#define led_pin2 GPIO_PIN_0
#define led_pin3 GPIO_PIN_15
#define led_pin4 GPIO_PIN_14
#define led_pin5 GPIO_PIN_6
#define led_pin6 GPIO_PIN_7
#define led_pin7 GPIO_PIN_1
/*---------- type define ----------*/
typedef enum {
    led_white_id1 = 1,
    led_white_id2,
    led_white_id3,
    led_white_id4,
    led_yellow_id1,
    led_yellow_id2,
    led_yellow_id3,
    led_yellow_id4,
    s_id_null = 0xff,
} s_id;
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
static bool bsp_init(void);
static void bsp_deinit(void);
static void bsp_set(unsigned char num, unsigned char val);
static unsigned char bsp_get(unsigned char num);

/*---------- variable ----------*/
static gpio_group_describe_t led_Lisi = {
    .group_name = "led_Lisi",
    .ops.init = bsp_init,
    .ops.deinit = bsp_deinit,
    .ops.get = bsp_get,
    .ops.set = bsp_set,
};
DEVICE_DEFINED(led_Lisi, gpio_group, &led_Lisi);

/*---------- function ----------*/
static bool bsp_init(void)
{
    rcu_periph_clock_enable(led_rcu0);
    rcu_periph_clock_enable(led_rcu1);

    gpio_init(led_port0, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, led_pin1 | led_pin8);
    gpio_bit_reset(led_port0, led_pin1 | led_pin8);
    gpio_init(led_port0, GPIO_MODE_IPD, GPIO_OSPEED_50MHZ, radar_pin);
    gpio_init(led_port1, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, led_pin2 | led_pin3 | led_pin4 | led_pin5 | led_pin6 | led_pin7);
    gpio_bit_reset(led_port1, led_pin2 | led_pin3 | led_pin4 | led_pin5 | led_pin6 | led_pin7);
    
//    gpio_bit_set(led_port0, led_pin1);
//    gpio_bit_set(led_port1, led_pin2);
//    gpio_bit_set(led_port1, led_pin3);
//    gpio_bit_set(led_port1, led_pin4);
//    gpio_bit_set(led_port1, led_pin5);
//    gpio_bit_set(led_port1, led_pin6);
//    gpio_bit_set(led_port1, led_pin7);
//    gpio_bit_set(led_port0, led_pin8);
// 
    return true;
}

static void bsp_deinit(void)
{
    gpio_init(led_port0, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, radar_pin);
    gpio_init(led_port0, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, led_pin1 | led_pin8);
    gpio_init(led_port1, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, led_pin2 | led_pin3 | led_pin4 | led_pin5 | led_pin6 | led_pin7);
    gpio_bit_reset(led_port0, led_pin1 | led_pin8);
    gpio_bit_reset(led_port1, led_pin2 | led_pin3 | led_pin4 | led_pin5 | led_pin6 | led_pin7);

}

static void bsp_set(unsigned char num, unsigned char val)
{
    if (val == 0) {
        if (led_white_id1 == num)
            gpio_bit_reset(led_port0, led_pin1);
        else if (led_white_id2 == num)
            gpio_bit_reset(led_port1, led_pin2);
        else if (led_white_id3 == num)
            gpio_bit_reset(led_port1, led_pin3);
        else if (led_white_id4 == num)
            gpio_bit_reset(led_port1, led_pin4);
        else if (led_yellow_id1 == num)
            gpio_bit_reset(led_port1, led_pin5);
        else if (led_yellow_id2 == num)
            gpio_bit_reset(led_port1, led_pin6);
        else if (led_yellow_id3 == num)
            gpio_bit_reset(led_port1, led_pin7);
        else if (led_yellow_id4 == num)
            gpio_bit_reset(led_port0, led_pin8);
    }
    else {
        if (led_white_id1 == num)
            gpio_bit_set(led_port0, led_pin1);
        else if (led_white_id2 == num)
            gpio_bit_set(led_port1, led_pin2);
        else if (led_white_id3 == num)
            gpio_bit_set(led_port1, led_pin3);
        else if (led_white_id4 == num)
            gpio_bit_set(led_port1, led_pin4);
        else if (led_yellow_id1 == num)
            gpio_bit_set(led_port1, led_pin5);
        else if (led_yellow_id2 == num)
            gpio_bit_set(led_port1, led_pin6);
        else if (led_yellow_id3 == num)
            gpio_bit_set(led_port1, led_pin7);
        else if (led_yellow_id4 == num)
            gpio_bit_set(led_port0, led_pin8);
    }
}
static unsigned char bsp_get(unsigned char num) {
    return gpio_input_bit_get(led_port0, radar_pin);
}